<?php

namespace App\Http\Controllers\Template;

use App\Http\Controllers\BaseController;
use App\Http\Validate\Template\TemplateDispatchnote as TemplateTemplateDispatchnote;
use App\Models\Template\TemplateDispatchnote;
use Illuminate\Http\Request;

/**
 * 报车单模版
 */
class TemplateDispatchnoteController extends BaseController
{
    /**
     * 获取列表
     */
    public function getlist(Request $request)
    {
        $status = $request->status ?? 0;
        $isgetall = $request->isgetall ?? 0;


        $list = TemplateDispatchnote::getList($request, $this->mangerinfo['cpid'], $status, $isgetall);
        return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => $list]);
    }

    /**
     * 新增报车单模版
     */
    public function dopostadd(Request $request)
    {
        $req_data = $request->only(['dispatchnote_no', 'warehouse_name', 'dispatchnote_filepath']);

        $Validate = new TemplateTemplateDispatchnote();
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }
        //当前账套内
        $isexist = TemplateDispatchnote::where([['cpid', $this->mangerinfo['cpid']], ['dispatchnote_no', $req_data['dispatchnote_no']]])->first();

        if ($isexist) {
            return response()->json(['code' => '001', 'msg' => '编码不允许重复']);
        }

        $data = [
            'cpid' => $this->mangerinfo['cpid'],
            'dispatchnote_no' => $req_data['dispatchnote_no'],
            'warehouse_name' => $req_data['warehouse_name'],
            'dispatchnote_filepath' => $req_data['dispatchnote_filepath'],
            'update_at' => time()
        ];
        $data['create_at'] = time();
        TemplateDispatchnote::create($data)->id;

        return response()->json(['code' => '000', 'msg' => '新增成功']);
    }
    /**
     * 冻结
     */
    public function changestatus(Request $request)
    {
        $id = $request->id ?? 0;
        $info = TemplateDispatchnote::where([['id', $id], ['cpid', $this->mangerinfo['cpid']]])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        $status = 2;
        TemplateDispatchnote::where([['id', $id]])->update(['status' => $status, 'update_at' => time()]);
        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }
}
